﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

/// <summary>
///SqlInjectionFilter 的摘要说明
/// </summary>
public class SqlInjectionFilter
{
	public SqlInjectionFilter()
	{
	}
    #region 过滤特殊字符
    /// <summary>
    /// 过滤特殊字符
    /// </summary>
    /// <param name="inputStr">字符串</param>
    /// <returns>string</returns>
    public static string cutBadStr(string inputStr)
    {
        inputStr = inputStr.ToLower().Replace(",", "");
        inputStr = inputStr.ToLower().Replace("<", "&lt;");
        inputStr = inputStr.ToLower().Replace(">", "&gt;");
        inputStr = inputStr.ToLower().Replace("%", "");
        inputStr = inputStr.ToLower().Replace(".", "");
        inputStr = inputStr.ToLower().Replace(":", "");
        inputStr = inputStr.ToLower().Replace("#", "");
        inputStr = inputStr.ToLower().Replace("&", "");
        inputStr = inputStr.ToLower().Replace("$", "");
        inputStr = inputStr.ToLower().Replace("^", "");
        inputStr = inputStr.ToLower().Replace("*", "");
        inputStr = inputStr.ToLower().Replace("`", "");
        inputStr = inputStr.ToLower().Replace(" ", "");
        inputStr = inputStr.ToLower().Replace("~", "");
        inputStr = inputStr.ToLower().Replace("or", "");
        inputStr = inputStr.ToLower().Replace("and", "");
        return inputStr;
    }
    #endregion
    //过滤html文本
    public static string FilterHtml(string html)
    {
        Regex regex1 = new Regex(@"<script[sS]+</script *>", RegexOptions.IgnoreCase);
        Regex regex2 = new Regex(@" href *= *[sS]*script *:", RegexOptions.IgnoreCase);
        Regex regex3 = new Regex(@" no[sS]*=", RegexOptions.IgnoreCase);
        Regex regex4 = new Regex(@"<iframe[sS]+</iframe *>", RegexOptions.IgnoreCase);
        Regex regex5 = new Regex(@"<frameset[sS]+</frameset *>", RegexOptions.IgnoreCase);
        Regex regex6 = new Regex(@"<img[^>]+>", RegexOptions.IgnoreCase);
        Regex regex7 = new Regex(@"</p>", RegexOptions.IgnoreCase);
        Regex regex8 = new Regex(@"<p>", RegexOptions.IgnoreCase);
        Regex regex9 = new Regex(@"<[^>]*>", RegexOptions.IgnoreCase);
        html = regex1.Replace(html, ""); //过滤<script></script>标记 
        html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性 
        html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 
        html = regex4.Replace(html, ""); //过滤iframe html = regex5.Replace(html, ""); //过滤frameset 
        html = regex6.Replace(html, ""); //过滤frameset 
        html = regex7.Replace(html, ""); //过滤frameset 
        html = regex8.Replace(html, ""); //过滤frameset 
        html = regex9.Replace(html, "");
        html = html.Replace(" ", "");
        html = html.Replace("</strong>", "");
        html = html.Replace("<strong>", "");
        return html;

    }

}
